//
//  TopMenuView.m
//  PYWGetColor
//
//  Created by 杜文杰 on 2018/6/7.
//  Copyright © 2018年 dwj. All rights reserved.
//

#import "TopMenuView.h"
#define MARGINWIDTH 10
@interface TopMenuView()
@property (nonatomic, strong) UIButton *firstBtn;
@property (nonatomic, strong) UIButton *secondBtn;
@property (nonatomic, strong) UIButton *thirdBtn;

@end

@implementation TopMenuView
- (void)setStateStr:(NSString *)stateStr
{
    if ([stateStr isEqualToString:@"1"]) {
        //   我的需求
        [self.firstBtn setImage:[UIImage imageNamed:@"list_sj.1"] forState:UIControlStateNormal];
        [self.firstBtn setTitleColor:getColor(textColor) forState:UIControlStateNormal];
        self.firstBtn.selected = NO;
        [self.thirdBtn setImage:[UIImage imageNamed:@"list_sj.1"] forState:UIControlStateNormal];
        [self.thirdBtn setTitleColor:getColor(textColor) forState:UIControlStateNormal];
        self.thirdBtn.selected = NO;
        
        self.secondBtn.selected = !self.secondBtn.selected;
        if (self.secondBtn.isSelected) {
            [self.secondBtn setImage:[UIImage imageNamed:@"list_sj.2"] forState:UIControlStateNormal];
            [self.secondBtn setTitleColor:getColor(mainColor) forState:UIControlStateNormal];
        }else{
            [self.secondBtn setImage:[UIImage imageNamed:@"list_sj.1"] forState:UIControlStateNormal];
            [self.secondBtn setTitleColor:getColor(textColor) forState:UIControlStateNormal];
        }
//        self.btnTagBlock(@"2");
    }
    
    if ([stateStr isEqualToString:@"2"])
    {
        // 我的服务
        [self.firstBtn setImage:[UIImage imageNamed:@"list_sj.1"] forState:UIControlStateNormal];
        [self.firstBtn setTitleColor:getColor(textColor) forState:UIControlStateNormal];
        self.firstBtn.selected = NO;
        [self.secondBtn setImage:[UIImage imageNamed:@"list_sj.1"] forState:UIControlStateNormal];
        [self.secondBtn setTitleColor:getColor(textColor) forState:UIControlStateNormal];
        self.secondBtn.selected = NO;
        
        self.thirdBtn.selected = !self.thirdBtn.selected;
        if (self.thirdBtn.isSelected) {
            [self.thirdBtn setImage:[UIImage imageNamed:@"list_sj.2"] forState:UIControlStateNormal];
            [self.thirdBtn setTitleColor:getColor(mainColor) forState:UIControlStateNormal];
        }else{
            [self.thirdBtn setImage:[UIImage imageNamed:@"list_sj.1"] forState:UIControlStateNormal];
            [self.thirdBtn setTitleColor:getColor(textColor) forState:UIControlStateNormal];
        }
//        self.btnTagBlock(@"3");
    }
}
- (void)firstAction:(UIButton *)btn
{
    NSLog(@"我要抢单");
    if ([btn isEqual:self.firstBtn]) {
        [self.secondBtn setImage:[UIImage imageNamed:@"list_sj.1"] forState:UIControlStateNormal];
        [self.secondBtn setTitleColor:getColor(textColor) forState:UIControlStateNormal];
        self.secondBtn.selected = NO;
        [self.thirdBtn setImage:[UIImage imageNamed:@"list_sj.1"] forState:UIControlStateNormal];
        [self.thirdBtn setTitleColor:getColor(textColor) forState:UIControlStateNormal];
        self.thirdBtn.selected = NO;
        
        self.firstBtn.selected = !self.firstBtn.selected;
        if (self.firstBtn.isSelected) {
            [self.firstBtn setImage:[UIImage imageNamed:@"list_sj.2"] forState:UIControlStateNormal];
            [self.firstBtn setTitleColor:getColor(mainColor) forState:UIControlStateNormal];
        }else{
            [self.firstBtn setImage:[UIImage imageNamed:@"list_sj.1"] forState:UIControlStateNormal];
            [self.firstBtn setTitleColor:getColor(textColor) forState:UIControlStateNormal];
        }
        self.btnTagBlock(@"1");
    }
    if ([btn isEqual:self.secondBtn]) {
        [self.firstBtn setImage:[UIImage imageNamed:@"list_sj.1"] forState:UIControlStateNormal];
        [self.firstBtn setTitleColor:getColor(textColor) forState:UIControlStateNormal];
        self.firstBtn.selected = NO;
        [self.thirdBtn setImage:[UIImage imageNamed:@"list_sj.1"] forState:UIControlStateNormal];
        [self.thirdBtn setTitleColor:getColor(textColor) forState:UIControlStateNormal];
        self.thirdBtn.selected = NO;
        
        self.secondBtn.selected = !self.secondBtn.selected;
        if (self.secondBtn.isSelected) {
            [self.secondBtn setImage:[UIImage imageNamed:@"list_sj.2"] forState:UIControlStateNormal];
            [self.secondBtn setTitleColor:getColor(mainColor) forState:UIControlStateNormal];
        }else{
            [self.secondBtn setImage:[UIImage imageNamed:@"list_sj.1"] forState:UIControlStateNormal];
            [self.secondBtn setTitleColor:getColor(textColor) forState:UIControlStateNormal];
        }
        self.btnTagBlock(@"2");
    }
    
    if ([btn isEqual:self.thirdBtn]) {
        [self.firstBtn setImage:[UIImage imageNamed:@"list_sj.1"] forState:UIControlStateNormal];
        [self.firstBtn setTitleColor:getColor(textColor) forState:UIControlStateNormal];
        self.firstBtn.selected = NO;
        [self.secondBtn setImage:[UIImage imageNamed:@"list_sj.1"] forState:UIControlStateNormal];
        [self.secondBtn setTitleColor:getColor(textColor) forState:UIControlStateNormal];
        self.secondBtn.selected = NO;
        
        self.thirdBtn.selected = !self.secondBtn.selected;
        if (self.thirdBtn.isSelected) {
            [self.thirdBtn setImage:[UIImage imageNamed:@"list_sj.2"] forState:UIControlStateNormal];
            [self.thirdBtn setTitleColor:getColor(mainColor) forState:UIControlStateNormal];
        }else{
            [self.thirdBtn setImage:[UIImage imageNamed:@"list_sj.1"] forState:UIControlStateNormal];
            [self.thirdBtn setTitleColor:getColor(textColor) forState:UIControlStateNormal];
        }
        self.btnTagBlock(@"3");
    }
}
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUpView];
        
        [self.secondBtn setImage:[UIImage imageNamed:@"list_sj.1"] forState:UIControlStateNormal];
        [self.secondBtn setTitleColor:getColor(textColor) forState:UIControlStateNormal];
        self.secondBtn.selected = NO;
        [self.thirdBtn setImage:[UIImage imageNamed:@"list_sj.1"] forState:UIControlStateNormal];
        [self.thirdBtn setTitleColor:getColor(textColor) forState:UIControlStateNormal];
        self.thirdBtn.selected = NO;
        
        self.firstBtn.selected = !self.firstBtn.selected;
        if (self.firstBtn.isSelected) {
            [self.firstBtn setImage:[UIImage imageNamed:@"list_sj.2"] forState:UIControlStateNormal];
            [self.firstBtn setTitleColor:getColor(mainColor) forState:UIControlStateNormal];
        }else{
            [self.firstBtn setImage:[UIImage imageNamed:@"list_sj.1"] forState:UIControlStateNormal];
            [self.firstBtn setTitleColor:getColor(textColor) forState:UIControlStateNormal];
        }
    }
    return self;
}
- (void)setUpView
{
    UIButton *firstBtn = [[UIButton alloc]init];
    firstBtn.backgroundColor = [UIColor clearColor];
    [firstBtn setTitle:@"我要抢单" forState:UIControlStateNormal];
    [firstBtn setTitleColor:getColor(textColor) forState:UIControlStateNormal];
    [firstBtn setImage:[UIImage imageNamed:@"list_sj.1"] forState:UIControlStateNormal];
    [firstBtn addTarget:self action:@selector(firstAction:) forControlEvents:UIControlEventTouchUpInside];
    [firstBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -135 / WIDTH_6S_SCALE)];
    [firstBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -20 / WIDTH_6S_SCALE, 0, 0)];
    firstBtn.titleLabel.font = DEF_FontSize_14;
    self.firstBtn = firstBtn;
    [self addSubview:firstBtn];
    
    UIButton *secondBtn = [[UIButton alloc]init];
    secondBtn.backgroundColor = [UIColor clearColor];
    [secondBtn setTitle:@"我的需求" forState:UIControlStateNormal];
    [secondBtn setTitleColor:getColor(textColor) forState:UIControlStateNormal];
    [secondBtn setImage:[UIImage imageNamed:@"list_sj.1"] forState:UIControlStateNormal];
    [secondBtn addTarget:self action:@selector(firstAction:) forControlEvents:UIControlEventTouchUpInside];
    [secondBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -135 / WIDTH_6S_SCALE)];
    [secondBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -20 / WIDTH_6S_SCALE, 0, 0)];
    secondBtn.titleLabel.font = DEF_FontSize_14;
    self.secondBtn = secondBtn;
    [self addSubview:secondBtn];
    
    UIButton *thirdBtn = [[UIButton alloc]init];
    thirdBtn.backgroundColor = [UIColor clearColor];
    [thirdBtn setTitle:@"我的服务" forState:UIControlStateNormal];
    [thirdBtn setTitleColor:getColor(textColor) forState:UIControlStateNormal];
    [thirdBtn setImage:[UIImage imageNamed:@"list_sj.1"] forState:UIControlStateNormal];
    [thirdBtn addTarget:self action:@selector(firstAction:) forControlEvents:UIControlEventTouchUpInside];
    [thirdBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -135 / WIDTH_6S_SCALE)];
    [thirdBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -20, 0, 0)];
    thirdBtn.titleLabel.font = DEF_FontSize_14;
    self.thirdBtn = thirdBtn;
    [self addSubview:thirdBtn];
    
    __weak typeof(self)weakself = self;
    [firstBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakself.mas_centerY);
        make.left.equalTo(weakself.mas_left).offset(10 / WIDTH_6S_SCALE);
        make.width.mas_equalTo((SCREEN_WIDTH - 4 * MARGINWIDTH)/ 3 );
        make.height.mas_equalTo(20 / HEIGHT_6S_SCALE);
    }];
    [secondBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakself.mas_centerY);
        make.left.equalTo(firstBtn.mas_right).offset(10 / WIDTH_6S_SCALE);
        make.width.mas_equalTo((SCREEN_WIDTH - 4 * MARGINWIDTH)/ 3 );
        make.height.mas_equalTo(20 / HEIGHT_6S_SCALE);
    }];
    [thirdBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakself.mas_centerY);
        make.left.equalTo(secondBtn.mas_right).offset(10 / WIDTH_6S_SCALE);
        make.width.mas_equalTo((SCREEN_WIDTH - 4 * MARGINWIDTH)/ 3 );
        make.height.mas_equalTo(20 / HEIGHT_6S_SCALE);
    }];
    
    UIView *lineView1 = [[UIView alloc]init];
    lineView1.backgroundColor = getColor(dividerColor);
    [self addSubview:lineView1];
    [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(firstBtn.mas_right).offset(5 / WIDTH_6S_SCALE);
        make.centerY.equalTo(firstBtn.mas_centerY);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(firstBtn.mas_height);
    }];
    UIView *lineView2 = [[UIView alloc]init];
    lineView2.backgroundColor = getColor(dividerColor);
    [self addSubview:lineView2];
    [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(secondBtn.mas_right).offset(5 / WIDTH_6S_SCALE);
        make.centerY.equalTo(secondBtn.mas_centerY);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(secondBtn.mas_height);
    }];
}
@end
